package zxr.demo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import zxr.Student;
import zxr.service.StudentService;

import java.io.IOException;
import java.io.InputStream;


public class Rundemo2 {


    public void run(Student student) throws IOException {
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        //获取SqlsessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //获取SqlsessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        //获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);//这里将opensession设置成true，即自动提交事务
        //获取mapper接口对象
        //注意这里我们的mapper里面只有接口没有实体类，所以通过getMapper的方法来获取该接口实体类的对象
        //getMapper方法在底层会构建一个实体类，并将该实体类的对象返回
        StudentService studentService = sqlSession.getMapper(StudentService.class);

        studentService.updateById(student);

        sqlSession.commit();


    }

}
